package com.monika.main.system.util;


import com.monika.main.system.domain.common.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 *
 * @Author whh
 * @Date: 2023/04/14/ 21:00
 * @description
 */

@Component
public class RedisCache {


    /**
     *缓存基本对象 Integer、String、实体类
     * @param key
     * @param val
     * @param <K>
     * @param <V>
     */
    public static <K, V> void put(K key, V val) {
        redisTemplate.opsForValue().set(key,val);
    }


    /**
     *获取基本对象
     * @param key
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> V get(K key) {
        return (V) redisTemplate.opsForValue().get(key);
    }

    /**
     *缓存基本对象 Integer、String、实体类 并设置key的有效期
     * @param key
     * @param val
     * @param timout
     * @param timeUnit
     * @param <K>
     * @param <V>
     */
    public static <K, V> void put(K key, V val, long timout, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key,val,timout,timeUnit);
    }


    /**
     * 缓存List集合
     * @param key
     * @param val
     * @param <K>
     * @param <V>
     */
    public static  <K,V> void putList(K key, List<V> val){
        redisTemplate.opsForList().rightPushAll(key,val);
    }


    /**
     * 获取缓存的集合
     * @param key
     * @param <K>
     * @param <V>
     * @return
     */
    public static  <K,V>  List<V> getList(K key){
        return redisTemplate.opsForList().range(key,0,-1);
    }


    /**
     * 缓存set
     * @param key
     * @param dataSet
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K,V> BoundSetOperations<K, V> putSet(final K key, final Set<V> dataSet)
    {
        BoundSetOperations<K, V> setOperation = redisTemplate.boundSetOps(key);
        Iterator<V> it = dataSet.iterator();
        while (it.hasNext())
        {
            setOperation.add(it.next());
        }
        return setOperation;
    }


    /**
     * 获取set
     * @param key
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K,V> Set<V> getSet(final K key)
    {
        return redisTemplate.opsForSet().members(key);
    }


    /**
     * 缓存map
     * @param key
     * @param dataMap
     * @param <K>
     * @param <V>
     */
    public static <K,V> void putMap(final K key, final Map<String, V> dataMap)
    {
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key, dataMap);
        }
    }

    /**
     * 获得缓存的map
     * @param key
     * @return
     */
    public static <K,V> Map<String, V> getMap(final K key)
    {
        return redisTemplate.opsForHash().entries(key);
    }
    /**
     * 设置key的有效时间
     * @param key
     * @param timeout
     * @param timeUnit
     * @param <K>
     * @return
     */
    public static <K> boolean expire(K key,long timeout,TimeUnit timeUnit){
        return redisTemplate.expire(key,timeout,timeUnit);

    }

    /**
     * 获取key的有效时间
     * @param key
     * @param <K>
     * @return
     */
    public static <K> long getExpire(K key){
        return redisTemplate.getExpire(key);

    }


    /**
     * 删除key
     * @param key
     * @param <K>
     * @return
     */
    public static <K> boolean delete(K key){
        return redisTemplate.delete(key);
    }



    /**
     * key是否存在
     * @param key
     * @param <K>
     * @return
     */
    public static <K> boolean hasKey(K key){
        return redisTemplate.hasKey(key);
    }


    private static RedisTemplate redisTemplate;

    @Autowired
    @Qualifier("redisTemplate")
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisCache.redisTemplate = redisTemplate;
    }
}




